import { createPage } from '@mpxjs/core'
import {AlbumBrief, Song} from "../../services/types";
import {WrapWidthUndefined} from "../../utils/typies";
import {songListOfAlbum} from "../../services/song.service";

interface SongListVo {
  data: {
    album: WrapWidthUndefined<AlbumBrief>;
    songs: Song[];
  }
}

createPage<SongListVo['data']>({
  data: {
    album: undefined,
    songs: []
  },
  onLoad () {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.once('albumBriefFromRecommend', (album: AlbumBrief) => {
      if (album) {
        this.album = album;
        wx.setNavigationBarTitle({ title: album.dissname });
        songListOfAlbum(album.dissid).then(songs => {
          this.songs = songs.map(item => ({
            ...item,
            singerStr: item.singer.map(singer => singer.name).join('/')
          }));
        });
      }
    });
  },
  toPlay(song: Song): void {
    wx.navigateTo({
      url: '/pages/play-song/index',
      success({ eventChannel }) {
        eventChannel.emit('songBriefFromList', song);
      }
    })
  }
});
